export function uuid() {
  let res = ''
  let randomVal = 0
  for (let i = 0; i < 32; i++) {
    randomVal = (Math.random() * 16) | 0

    if (i == 8 || i == 12 || i == 16 || i == 20) {
      res += '-'
    }
    res += (i == 12 ? 4 : i == 16 ? (randomVal & 3) | 8 : randomVal).toString(16)
  }
  return res
}

export function delay(ms: number) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms)
  })
}

function padTo2Digits(num: number) {
  return num.toString().padStart(2, '0')
}

export function formatDate(date: Date) {
  return (
    [date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate())].join(
      '-'
    ) +
    ' ' +
    [
      padTo2Digits(date.getHours()),
      padTo2Digits(date.getMinutes()),
      padTo2Digits(date.getSeconds())
    ].join(':')
  )
}
